package com.shiwaixiangcun.monitor.config;

import com.github.tobato.fastdfs.conn.*;
import com.github.tobato.fastdfs.domain.DefaultThumbImageConfig;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.DefaultFastFileStorageClient;
import com.github.tobato.fastdfs.service.DefaultTrackerClient;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.github.tobato.fastdfs.service.TrackerClient;
import com.shiwaixiangcun.core.plugin.upload.FileService;
import com.shiwaixiangcun.core.plugin.upload.impl.FastdfsFileServiceImpl;
import com.shiwaixiangcun.core.repository.file.UploadFileInfoRepository;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.AuditorAware;
import org.springframework.jmx.support.RegistrationPolicy;

import java.util.Arrays;

@Configuration
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) // 解决jmx重复注册bean的问题
public class FastdfsConfig implements EnvironmentAware {


    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }


    //////////////////////////// fastdfs config start //////////////////////////////
    ////////////////////配置连接池
    private PooledConnectionFactory buildPooledConnectionFactory() {
        PooledConnectionFactory connectionFactory = new PooledConnectionFactory();
        Integer soTimeout = environment.getRequiredProperty("fdfs.soTimeout", Integer.class);
        Integer connectTimeout = environment.getRequiredProperty("fdfs.connectTimeout", Integer.class);
        String charsetName = environment.getRequiredProperty("fdfs.charsetName", String.class);

        connectionFactory.setSoTimeout(soTimeout);
        connectionFactory.setConnectTimeout(connectTimeout);
        connectionFactory.setCharsetName(charsetName);

        return connectionFactory;
    }

    private ConnectionPoolConfig buildConnectionPoolConfig() {
        return new ConnectionPoolConfig();
    }

    @Bean
    public FdfsConnectionPool fdfsConnectionPool() {
        PooledConnectionFactory factory = buildPooledConnectionFactory();
        ConnectionPoolConfig poolConfig = buildConnectionPoolConfig();
        return new FdfsConnectionPool(factory, poolConfig);
    }

    @Bean
    public ConnectionManager connectionManager(FdfsConnectionPool connectionPool) {
        return new ConnectionManager(connectionPool);
    }

    ///////////////////配置Tracker连接池
    @Bean
    public TrackerConnectionManager trackerConnectionManager(FdfsConnectionPool connectionPool) {
        TrackerConnectionManager connectionManager = new TrackerConnectionManager(connectionPool);
        String[] trackers = environment.getRequiredProperty("fdfs.trackerList").split("\\|");
        connectionManager.setTrackerList(Arrays.asList(trackers));
        return connectionManager;
    }

    @Bean
    public TrackerClient trackerClient(TrackerConnectionManager connectionManager) {
        DefaultTrackerClient trackerClient = new DefaultTrackerClient();
        trackerClient.setTrackerConnectionManager(connectionManager);
        return trackerClient;
    }


    /////////////////// 配置缩略图
    @Bean
    public ThumbImageConfig thumbImageConfig() {
        Integer width = environment.getRequiredProperty("fdfs.thumbImage.width", Integer.class);
        Integer height = environment.getRequiredProperty("fdfs.thumbImage.height", Integer.class);
        return new DefaultThumbImageConfig(width, height);
    }


    @Bean
    public FastFileStorageClient fastFileStorageClient(ThumbImageConfig thumbImageConfig, TrackerClient trackerClient, ConnectionManager connectionManager) {
        DefaultFastFileStorageClient storageClient = new DefaultFastFileStorageClient();
        storageClient.setDefaultThumbImageConfig(thumbImageConfig);
        storageClient.setTrackerClientService(trackerClient);
        storageClient.setConnectionManager(connectionManager);
        return storageClient;
    }

    //////////////////////////// fastdfs config end //////////////////////////////

    @Bean
    public FileService fileService(UploadFileInfoRepository repository, FastFileStorageClient storageClient, AuditorAware<Long> auditorAware) {
        Integer width = environment.getRequiredProperty("fdfs.thumbImage.width", Integer.class);
        Integer height = environment.getRequiredProperty("fdfs.thumbImage.height", Integer.class);
        FastdfsFileServiceImpl fileService = new FastdfsFileServiceImpl(repository);
        fileService.setStorageClient(storageClient);
        fileService.setAuditorAware(auditorAware);
        fileService.setThumbImageWidth(width);
        fileService.setThumbImageHeight(height);
        fileService.setResourceServerHost(environment.getRequiredProperty("resource.server.host"));
        return fileService;
    }

    //////////////////fastdfs end/////////////


}
